package com.third.share.umeng;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Gravity;

import com.third.login.ThirdLoginPlatform;
import com.third.share.ShareData;
import com.third.share.ThirdShareBase;
import com.umeng.social.tool.UMImageMark;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMEmoji;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.media.UMusic;
import com.umeng.socialize.utils.SocializeUtils;

import c.umengthirdlogin.R;

public class UMThirdShareBase extends ThirdShareBase{
    protected UMThirdShareListener mUMThirdShareListener;
    protected UMImage mImageurl;
    protected UMImage mImagelocal;
    protected UMVideo mVideo;
    protected UMusic mMusic;
    protected UMEmoji mEmoji;
    protected UMWeb mWeb;
    protected File mFile;
	protected SHARE_MEDIA mShareMedia;
    protected ArrayList<Integer> mStyleList = new ArrayList<Integer>();
	public UMThirdShareBase(Activity act) {
		super(act);
		// TODO Auto-generated constructor stub
		mUMThirdShareListener = new UMThirdShareListener();
		initMedia();
		init();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		   UMShareAPI.get(mActivity).onActivityResult(requestCode,resultCode,data);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		UMShareAPI.get(mActivity).release();
	}

	@Override
	public void thirdShare(ShareData shareData) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void cancelShare() {
		// TODO Auto-generated method stub
		
	}
	
	private class UMThirdShareListener implements UMShareListener {

		@Override
		public void onCancel(SHARE_MEDIA arg0) {
			// TODO Auto-generated method stub
			if(mThirdShareListener != null){
				mThirdShareListener.onCancel(mPlatform);
			}
			Log.e("share","UMThirdShareBase onCancel");
		}

		@Override
		public void onError(SHARE_MEDIA arg0, Throwable arg1) {
			// TODO Auto-generated method stub
			if(mThirdShareListener != null){
				mThirdShareListener.onError(mPlatform,arg1);
			}
			Log.e("share","UMThirdShareBase onError");
		}

		@Override
		public void onResult(SHARE_MEDIA arg0) {
			// TODO Auto-generated method stub
			if(mThirdShareListener != null){
				mThirdShareListener.onResult(mPlatform);
			}
			Log.e("share","UMThirdShareBase onResult");
		}

		@Override
		public void onStart(SHARE_MEDIA arg0) {
			// TODO Auto-generated method stub
			if(mThirdShareListener != null){
				mThirdShareListener.onStart(mPlatform);
			}
			Log.e("share","UMThirdShareBase onStart");
		}
	}

	 protected  void shareVideo(ShareData shareData){
		 if(shareData != null){
			 UMImage image = null;
			 if(shareData.mShareThumbStyle == ShareData.IMG_LOCAL_FILE){
				 File file = new File(shareData.mThumbFileUrl);
				 image = new UMImage(mActivity,file);
			 }else{
				 image = new UMImage(mActivity,shareData.mThumbFileUrl);
			 }
		//	 image = new UMImage()
			 UMVideo video = new UMVideo(shareData.mShareUrl);
			 video.setThumb(image);
			 video.setTitle(shareData.mShareTitle);
			 new ShareAction(mActivity).withMedia(video)
					 .setPlatform(mShareMedia)
					 .setCallback(mUMThirdShareListener).share();
		 }
	 }

	 protected void shareLocalImage(ShareData shareData){

	 }

	 protected  void shareImage(ShareData shareData){
		 if(shareData != null){
			 UMImage image = new UMImage(mActivity,shareData.mShareUrl);
			 int drawable = R.drawable.thumb;
			 image.setThumb(new UMImage(mActivity, drawable));
		//	 video.setThumb(image);
			 image.setTitle(shareData.mShareTitle);
			 new ShareAction(mActivity).withMedia(image)
					 .setPlatform(mShareMedia)
					 .setCallback(mUMThirdShareListener).share();
		 }
	 }
	 private void initMedia(){
	        UMImageMark umImageMark = new UMImageMark();
	        int drawable = R.drawable.thumb;
	        umImageMark.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
	        umImageMark.setMarkBitmap(BitmapFactory.decodeResource(mActivity.getResources(), R.drawable.umsocial_defaultwatermark));
	       // imagelocal = new UMImage(this,R.drawable.logo,umImageMark);
	        mImagelocal = new UMImage(mActivity,R.drawable.logo);
	        mImagelocal.setThumb(new UMImage(mActivity, drawable));
	        mMusic = new UMusic(Defaultcontent.musicurl);
	        mWeb = new UMWeb(Defaultcontent.url);
		// mWeb = new UMWeb(Defaultcontent.videourl);
	        mWeb.setTitle("This is web title");
	        mWeb.setThumb(new UMImage(mActivity,drawable));
	        mWeb.setDescription("my description");

	        mMusic.setTitle("This is music title");
	        mMusic.setThumb(new UMImage(mActivity, drawable));
	        mMusic.setDescription("my description");
	        mMusic.setmTargetUrl(Defaultcontent.url);
	        mEmoji = new UMEmoji(mActivity,"http://img5.imgtn.bdimg.com/it/u=2749190246,3857616763&fm=21&gp=0.jpg");
	        mEmoji.setThumb(new UMImage(mActivity, drawable));
	        mFile = new File(mActivity.getFilesDir()+"test.txt");
	        if (!mFile.exists()){
	            try {
	            	mFile.createNewFile();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        if (SocializeUtils.File2byte(mFile).length<=0){
	            String content = "U-share分享";
	            byte[] contentInBytes = content.getBytes();
	            try {
	                FileOutputStream fop = new FileOutputStream(mFile);
	                fop.write(contentInBytes);
	                fop.flush();
	                fop.close();

	            } catch (FileNotFoundException e) {
	                e.printStackTrace();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }

	        }
	    }
}
